﻿#region 命名空间

using System;
using System.Web.UI;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.WebControls;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI
{
    /// <summary>
    ///     Guestbook 的摘要说明。
    /// </summary>
    public partial class Guestbook : ClientPublicPage
    {
        private readonly GuestBookManage _myBLL = new GuestBookManage();
        private readonly IP2Location _address = new IP2Location();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var config = CMSConfig.Instance();
                Title = config.GuestBook.Name;


                if (Request["Action"] == "Add")
                {
                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                }
                else
                {
                    if (config.GuestBook.ShowGB == false)
                    {
                        Response.Redirect("GuestBook.aspx?Action=Add");
                    }
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;

                    Bind(1);
                }
            }
        }

        private void Bind(int PageNo)
        {
            var pp = new PagingParam();
            pp.PageSize = this.PN.PageSize;
            pp.PageNo = PageNo;

            this.DL_GuesetBook.DataSource = this._myBLL.BindList(pp);
            this.DL_GuesetBook.DataBind();
        }

        protected void Post_Click(object sender, EventArgs e)
        {
            var model = new GuestBook
            {
                Name = this.txtName.Text,
                Content = this.txtContent.Text,
                Home = this.txtHome.Text,
                EMail = this.txtEmail.Text,
                QQ = this.txtQQ.Text,
                IP = HttpClient.GetIP(),
                Face = "Images/Guest/face/" + this.selectimg.Value + ".gif",
                Humor = this.RadioButtonList1.SelectedValue,
                Privacy = this.cbkPrivacy.Checked ? 1 : 0
            };


            var res = this._myBLL.Add(model, this.txtPassCode.Text);

            if (string.IsNullOrEmpty(res.Errors) == false)
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
                return;
            }

            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            Bind(1);
        }

        protected string UBB(string strContent, string strPrivacy)
        {
            if (strPrivacy != "1")
            {
                return HTML.Ubb2Html(strContent);
            }
            return "<font color=#009966>給站长的悄悄话。</font>";
        }

        protected string ShowOr(string strReply, string strPrivacy)
        {
            if (strReply.Trim().Length == 0 || strPrivacy == "1")
            {
                return String.Empty;
            }
            return "<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<font color=#009966>站长回复：</font>" + strReply;
        }

        protected string Address(object IP)
        {
            this._address.IP = IP.ToString();
            return this._address.IPLocation();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            Bind(1);
        }

        protected void PN_PageChanging(object src, PageChangingEventArgs e)
        {
            Bind(e.NewPageIndex);
        }
    }
}